﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Jumpton.SFA.Server.BusinessRule.MasterData;
using Jumpton.SFA.Server.Entity;
using Jumpton.SFA.Server.BusinessRule;

public partial class Customer_Product_List_Customer : PageBase
{

    private const string REQUEST_LIST_ID = "listID";
    private int listID = 0;
    private const string RESPONSE_LIST_MAINTAIN_PAGE = "Customer_Product_List.aspx";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString[REQUEST_LIST_ID] != null)
        {
            int.TryParse(Request.QueryString[REQUEST_LIST_ID], out listID);
        }
        if (!IsPostBack)
        {
            this.InitForm();
            ddlSelected.SelectedValue = "1";
            BindGrid();
            ddlSelected.SelectedValue = "0";
        }
    }

    private void InitForm()
    {
        CUSTOMER_PRODUCT_LISTS list = new CustomerProductListManager().Load(listID);
        if (list != null)
        {
            lblListName.Text = list.NAME;
        }

        Jumpton.Persistent.ObjectTable ot;
        ddlChain.DataTextField = "DESCRIPTION";
        ddlChain.DataValueField = "CODE";
        ddlChain.DataSource = new ChainManager().GetCourseTable(this.MyStateManager.DomainID);
        ddlChain.DataBind();
        ddlChain.Items.Insert(0,new ListItem(Resources.language.ddl_All, ""));

        DictionaryItemsManager dim = new DictionaryItemsManager();
        //ddlClass.DataTextField = "Name";
        //ddlClass.DataValueField = "DictionaryItemId";
        //ddlClass.DataSource = dim.GetItemList(DictionaryEnum.CustomerClass, this.MyStateManager.DomainID);
        //ddlClass.DataBind();
        //ddlClass.Items.Insert(0, new ListItem(Resources.language.ddl_All, ""));

        ddlChannel.DataTextField = "Name";
        ddlChannel.DataValueField = "DictionaryItemId";
        ddlChannel.DataSource = dim.GetItemList(DictionaryEnum.CustomerChannel, this.MyStateManager.DomainID);
        ddlChannel.DataBind();
        ddlChannel.Items.Insert(0, new ListItem(Resources.language.ddl_All, ""));

        //区域层次
        ddlLevel.DataTextField = "Name";
        ddlLevel.DataValueField = "DictionaryItemId";
        ddlLevel.DataSource = dim.GetItemList(DictionaryEnum.ChannelType, this.MyStateManager.DomainID);
        ddlLevel.DataBind();
        ddlLevel.DataBind();
        ddlLevel.Items.Insert(0, new ListItem(Resources.language.ddl_All, ""));

    }

    private void BindGrid()
    {
        if (listID > 0)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder(" AND C.DOMAIN_ID='" + this.MyStateManager.DomainID + "' ");

            if (txtCustomer.Text != string.Empty)
            {
                sb.Append(" AND C.[CODE] LIKE '%" + txtCustomer.Text.Trim() + "%' or C.[NAME] LIKE '%" + txtCustomer.Text.Trim() + "%' AND C.DOMAIN_ID='" + this.MyStateManager.DomainID + "' ");
            }

            if (ddlChain.SelectedValue != string.Empty)
            {
                sb.Append(" And CHAIN_CODE='"+ddlChain.SelectedValue+"' ");
            }
            if (ddlChannel.SelectedValue != string.Empty)
            {
                sb.Append(" AND LOCAL_LEVEL1_CODE =  '"+ddlChannel.SelectedValue +"'  ");
            }
            //if (ddlClass.SelectedValue != string.Empty)
            //{
            //    sb.Append(" AND CUST_CLASS_CODE =  '" + ddlClass.SelectedValue + "'  ");
            //}
            if (ddlLevel.SelectedValue != string.Empty)
            {
                sb.Append(" AND GEO_LEVEL_CODE = '" + ddlLevel.SelectedValue + "' ");
            }

            DataSet ds = CustomerManager.SearchCustomerForProductList(listID, sb.ToString(),ddlSelected.SelectedValue);
            if (ds != null && ds.Tables.Count > 0)
            {
                GridPager1.DataSource = ds.Tables[0];
                GridPager1.GridView = GridView1;
                GridPager1.BindData();
                GridPager1.Visible = GridView1.Rows.Count > 0;
               
            }
        }
    }

    protected void btnQuery_Click(object sender, EventArgs e)
    {
        BindGrid();
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        decimal customerID=0;
        string LogContent = "";

        if (listID > 0)
        {
            try
            {
                for (int i = 0; i < GridView1.Rows.Count; i++)
                {
                    Control ctlLabelID = GridView1.Rows[i].FindControl("lblID");
                    Control ctl = GridView1.Rows[i].FindControl("cbxSelected");
                    CheckBox ckx = ctl as CheckBox;
                    Label lblID = ctlLabelID as Label;
                    decimal.TryParse(lblID.Text, out customerID);

                    if (ckx != null && customerID > 0 && lblID != null)
                    {
                        CustomerManager.AssignCustomerForProductList(ckx.Checked, customerID, this.listID);
                    }
                }
                ShowMessage("保存成功");
            }
            catch (Exception ex)
            {
                ShowMessage("保存失败：" + ex.Message);
                Jumpton.SFA.Server.BusinessRule.System.EventManager.CreateNewEvent(
                  Jumpton.SFA.Server.BusinessRule.System.LogType.GENERAL_EXCEPTION, this.MyStateManager.Name,
                  this.MyStateManager.UserID, "客户分销列表分配客户失败 - 异常:" + ex.Message);
            }
        }
       
           
    }

    protected void SelectALL(object sender, EventArgs e)
    {
        CheckBox chk = sender as CheckBox;
        if (chk == null)
            return;
        foreach (GridViewRow row in this.GridView1.Rows)
        {
            CheckBox checkbox = row.Cells[GridView1.Columns.Count - 1].FindControl("cbxSelected") as CheckBox;
            if (checkbox != null)
                checkbox.Checked = chk.Checked;
        }
    }

    protected void gvChannelType_RowDataBound(object sender, GridViewRowEventArgs e)
    {
       if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Grid_RowDataBound(sender,e);
            DataRowView rowView = (DataRowView)e.Row.DataItem;

            bool selected =rowView["Selected"].ToString()=="1"?true:false;

            Control ctlCheckBox = e.Row.FindControl("cbxSelected");
            Control ctlLabelID = e.Row.FindControl("lblID");


            Label lblID = ctlLabelID as Label;
            Label lblProductListName=e.Row.FindControl("lblListName") as Label;
            CheckBox cbxSelected = ctlCheckBox as CheckBox;

            if (lblID != null)
            {
                lblID.Text = rowView["ID"].ToString();
            }
            
            if (cbxSelected != null)
            {
                if (lblProductListName != null)
                {
                    cbxSelected.Attributes.Add("onClick", "CheckList('" + cbxSelected.ClientID + "','" + lblProductListName.ClientID + "','" + lblListName.Text + "')");
                }
                if (selected)
                {
                    cbxSelected.Checked = true; ;
                }
                else
                {
                    cbxSelected.Checked = false;
                }
            }
        }
    }

    public void GridViewToolbarUserControl1_OnGVTBSetColumnsButtonEvent(object sender, EventArgs e)
    {
        BindGrid();
    }

    protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    {
        //for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
        //{         
        //    CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("cbxSelected");
        //    if (CheckBox1.Checked == true)
        //    {
        //        cbox.Checked = true;
        //    }
        //    else
        //    {
        //        cbox.Checked = false;
        //    }
        //}
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        BindGrid();
    }
}